package org.gtiles.components.login.web;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.gtiles.components.login.authentication.IAuthenticatedUser;
import org.gtiles.components.login.authentication.impl.AuthenticatedUserImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * 
 * Title: 默认json格式处理类<br>
 * Description: <br>
 * 
 * @author GuoR
 * @createDate 2016年3月18日
 */
@Service("org.gtiles.componemts.login.web.DefaultLoginJsonRenderImpl")
public class DefaultLoginJsonRenderImpl implements ILoginJsonRender {

	@Override
	public void loginSuccess(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		IAuthenticatedUser authenticationImpl = (IAuthenticatedUser) request
				.getSession().getAttribute(IAuthenticatedUser.SESSION_ATTRNAME);
//		JsonSuccessObject jsonObject = new JsonSuccessObject();
		HashMap<String,Object> jsonObject = new HashMap<String,Object>();
		jsonObject.put("code", "2000");
		Object extendAccountInfo = ((AuthenticatedUserImpl) authenticationImpl).getExtendAccountInfo();
		//未设置账号扩展信息，仅将账号基本信息返回
		Map<String,Object> data=new HashMap<String,Object>();
		data.put("JSESSIONID",request.getSession().getId());
		data.put("entityID", authenticationImpl.getEntityID());
		data.put("loginName", authenticationImpl.getLoginID());
		data.put("userName", authenticationImpl.getName());
		if(ObjectUtils.isEmpty(extendAccountInfo)) {
//			jsonObject.setData(data);
			jsonObject.put("data", data);
		}else {
			data.put("extendAccountInfo", extendAccountInfo);
//			jsonObject.setData(data);
			jsonObject.put("data", data);
		}
		ObjectMapper mapper = new ObjectMapper();
		String writeValueAsString = mapper.writeValueAsString(jsonObject);
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json");
		response.getWriter().write(writeValueAsString);
	}
	@Override
	public void loginFailure(HttpServletRequest request,
			HttpServletResponse response) throws IOException{
//		JsonErrorObject jsonObject = new JsonErrorObject();
		HashMap<String,Object> jsonObject = new HashMap<String,Object>();
		jsonObject.put("code", "5000");
		String message = (String)request.getAttribute("captchafail");
		if(StringUtils.hasText(message)){
//			jsonObject.setMessage("captcha failure");
			jsonObject.put("message", "captcha failure");
		}else{
//			jsonObject.setMessage("login failure");
			jsonObject.put("message", "login failure");
		}
		ObjectMapper mapper = new ObjectMapper();
		String writeValueAsString = mapper.writeValueAsString(jsonObject);
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json");
		response.getWriter().write(writeValueAsString);
	}
	@Override
	public void logoutSuccess(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
//		JsonSuccessObject jsonObject = new JsonSuccessObject();
		HashMap<String,Object> jsonObject = new HashMap<String,Object>();
		jsonObject.put("code", "2000");
//		jsonObject.setMessage("logout success");
		jsonObject.put("message", "logout success");
		ObjectMapper mapper = new ObjectMapper();
		String writeValueAsString = mapper.writeValueAsString(jsonObject);
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json");
		response.getWriter().write(writeValueAsString);
	}
}
